/* $RCSFile: Shipping2Fixed.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice.ECommerce.BankGateway/Shipping2Fixed.cs $
 * $log$
 * Revision 8 2010/08/09 10:07:05  christian.surieux
 *   Added Log comment to all cs files
 */

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

using AddonNice.ECommerce;

namespace AddonNice.ECommerce.Gateways.Shipping
{
	/// <summary>
	/// Summary description for ShippingDB.
	/// </summary>
	public class Shipping2Fixed : ShippingGtwBase
	{
		/// <summary>
		/// Each ShippingObject must implement this name.
		/// Using this name ShippingGtwDataLoader can
		/// instantiate the class.
		/// </summary>
		public override string Name
		{
			get{return "FixedShipping2";}
		}

		/// <summary>
        /// Packing is necessary
		/// </summary>
		public override	bool NeedPackaging
		{
			get {   return false;}
		}

        public override bool IsElectronicDelivery
		{
			get {   return false;}
		}
        
        public override bool IsShippingDelivery
		{
			get {   return true;}
		}

        public override	int ShippingsID
		{
			get{	return 200;}
		}

		public override decimal TotalShipping
		{
			get
			{
				decimal	tot	=	0m;
				try 
				{
					if ( Rate > 0 )
						tot	=	Convert.ToDecimal(((double)TotalAmount)*Rate / 100.0);
				} 
				catch (System.OverflowException) 
				{
				}
				return PostprocessCheck(tot);
			}
		}

		decimal _TotalAmount	=	0;
		public virtual decimal TotalAmount
		{
			get
			{
				return _TotalAmount;
			}
		}

		public override bool ExistShipping(string wCartID, string wCountryID,string wCurrency)
		{
			Init(wCartID,wCountryID,wCurrency);
			return true;
		}

		/// <summary>
		/// Calculates the shipping cost of the cart.
		/// This will more likely change in future.
		/// </summary>
		public override decimal Calculate(string wCartID, string wCountry,string wCurrency,decimal Total)
		{
			Init(wCartID,wCountry,wCurrency);
			_TotalAmount				=	Total;
			return TotalShipping;
		}

		protected override decimal PostprocessCheck(decimal inputMoney)
		{
			if (inputMoney < MinValue)
				inputMoney = MinValue;

			if (inputMoney > MaxValue)
				inputMoney = MaxValue;

			return base.PostprocessCheck(inputMoney);
		}

		double m_rate = 0;
		/// <summary>
		/// Set here a value from 0 to 100.
		/// Shipping will be calculated as a percentage of the total cost.
		/// </summary>
		public double Rate
		{
			get
			{
				return m_rate;
			}
			set
			{
                if ( value < 100d )
				    m_rate = value;
			}
		}
	}
}
